#include <stdlib.h>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_ttf.h>
#include <string.h>
#include "stage11.h"
#define voi2 290
#define voi1 220
#define JUMP_POWER_MAX 12











  int main(int argc, char *argv[])
{
SDL_Surface *ecran = NULL,*back,*perso,*voit=NULL,*voit1=NULL,*bus=NULL,*tube=NULL,*moto=NULL,*bat=NULL,*batj=NULL,*batr=NULL,*bat1=NULL,*batt=NULL,*source=NULL,*destination=NULL;
SDL_Init(SDL_INIT_VIDEO);
ecran=SDL_SetVideoMode(600,600, 32, SDL_HWSURFACE);
SDL_WM_SetCaption("Jeu (y)", NULL);
back=IMG_Load("90.png");
perso=IMG_Load("perso.bmp");
voit=IMG_Load("obs11.bmp");//voitpolice
voit1=IMG_Load("obs3.bmp");//voitrouge
bus=IMG_Load("bus.bmp");//bus
moto=IMG_Load("persoo.bmp");//moto
bat=IMG_Load("vert.bmp");//batt
batt=IMG_Load("batterie.bmp");//col
batr=IMG_Load("rouge.bmp");//batt rouge
batj=IMG_Load("jaune.bmp"); //batt jaune


SDL_Rect posback,posperso,posvoit,posvoit1,posbus,posbat,postube,posmoto,posbatr,posbatc,obs1;
SDL_Event event;
int o=1,x,power=0,jump=0;
int bgx,bgy=150;
char ch[50];
int vit=5,l=0,l1=0,l2=0,i;
struct obstacle obs[30];
  


  
posperso.x=90;//perso
posperso.y=voi1;//perso



obs[0].x=1000;
obs[0].y=voi1;

obs[1].x=2000;
obs[1].y=voi2;

obs[2].x=2500;
obs[2].y=voi2;

obs[3].x=2900;
obs[3].y=voi1;

obs[4].x=3400;
obs[4].y=voi2;

obs[5].x=3900;
obs[5].y=voi2;




posbat.x=0;
posbat.y=150;

posbatr.x=500;
posbatr.y=voi2;

posbatc.x=1300;
posbatc.y=voi1;




apply_surface(0,150,back,ecran);
SDL_SetColorKey(perso, SDL_SRCCOLORKEY, SDL_MapRGB(perso->format, 255, 255, 255));
SDL_SetColorKey(voit, SDL_SRCCOLORKEY, SDL_MapRGB(voit->format, 255, 255, 255));
SDL_SetColorKey(voit1, SDL_SRCCOLORKEY, SDL_MapRGB(voit1->format, 255, 255, 255));
SDL_SetColorKey(bus, SDL_SRCCOLORKEY, SDL_MapRGB(bus->format, 255, 255, 255));
SDL_SetColorKey(moto, SDL_SRCCOLORKEY, SDL_MapRGB(moto->format, 255, 255, 255));
SDL_SetColorKey(bat, SDL_SRCCOLORKEY, SDL_MapRGB(moto->format, 255, 255, 255));
SDL_SetColorKey(batj, SDL_SRCCOLORKEY, SDL_MapRGB(moto->format, 255, 255, 255));
SDL_SetColorKey(batr, SDL_SRCCOLORKEY, SDL_MapRGB(moto->format, 255, 255, 255));
SDL_SetColorKey(batt, SDL_SRCCOLORKEY, SDL_MapRGB(moto->format, 255, 255, 255));


//SDL_Flip(ecran);

bat1=bat;
while(o)
{




  
//scrolling
      
      bgx -=vit;  
        x+=vit;
    sprintf(ch,"SCORE       : %d",x);
   
      if( bgx <= -back->w )
         {  bgx = 0; }
//
        //jump
  if(event.type == SDL_KEYDOWN)

     if(event.key.keysym.sym == SDLK_SPACE)  
     
        if(posperso.y+posperso.h >= 300)
        {
            
            jump = 1;
            power = JUMP_POWER_MAX;
        }


        if(jump)
        {

            posperso.y -= power;

            power--;


            if(power == 0)
            jump = 0;
        
        }

        


        else if(posperso.y+posperso.h < 300)
        {

            posperso.y += power;
            
            if(power < JUMP_POWER_MAX)
            power++;
        }
//
     // collision 
        for(i=0;i<5;i++)
        {
        if(((posperso.x+perso->w)>=(bgx+obs[i].x+40)&&(posperso.y==obs[i].y)))
        {shakeScreen(ecran);
          SDL_Delay(1000);
          i=8;
          o=0;
        }
       }
       
      
        if(((posperso.x+perso->w)==(bgx+posbatr.x))&&(posperso.y==posbatr.y))//col bat1
        {
          
                l=1;
               
        }
        if(((posperso.x+perso->w)==(bgx+posbatc.x))&&(posperso.y==posbatc.y))//col bat2
        {
                
                l1=1;
               
        }

        if((l==0)&&(bgx<-500))
          {bat=batj;}

         if(bgx<-1300)
          {if ((l1==0)&&(l==0))
            {bat=batr;} 
            if((l==1)&&(l1==0))
            {bat=batj;}
          if((l1==1)&&(l==1))
            {bat=bat;}
          } 
        
    
        apply_surface( bgx, bgy, back, ecran );
        apply_surface( (bgx + back->w), bgy, back, ecran );
        SDL_BlitSurface(perso,NULL,ecran,&posperso);
        SDL_BlitSurface(bat,NULL,ecran,&posbat);//blit score
  
      

        apply_surface( bgx+obs[0].x, obs[0].y, voit1, ecran );
        apply_surface( bgx+obs[1].x, obs[1].y, voit, ecran );
        apply_surface( bgx+obs[2].x, obs[2].y, bus, ecran );
        apply_surface( bgx+obs[3].x, obs[3].y, moto, ecran );
        apply_surface( bgx+obs[4].x, obs[4].y, tube, ecran );
        apply_surface( bgx+posbatc.x, posbatc.y, batt, ecran );//batterie colision
        apply_surface( bgx+posbatr.x, posbatr.y, batt, ecran );//batterie colision2
                                    SDL_Surface *txt=NULL;
             TTF_Font *police = NULL;
             SDL_Color couleur = {100, 0, 0};
             SDL_Rect pos;
            pos.x=150;
             pos.y=150;
            TTF_Init();
            police = TTF_OpenFont("1.TTF",30);
             txt = TTF_RenderText_Solid(police,ch, couleur);
             SDL_BlitSurface(txt, NULL, ecran, &pos); 

             TTF_CloseFont(police);   
             TTF_Quit();
            SDL_FreeSurface(txt);
        
    


     
      



      
     SDL_PollEvent(&event);
  switch(event.type)
      {
        case SDL_QUIT:
      o=0;
      break;
        case SDL_KEYDOWN:
            switch (event.key.keysym.sym)
            {
              
                 case SDLK_ESCAPE:
                 o=0;
                  break;

                 case SDLK_UP:
          if(posperso.y!=voi1)
            posperso.y=voi1;

                    SDL_BlitSurface(perso,NULL,ecran,&posperso);

            break;
          case SDLK_DOWN:
            if(posperso.y!=voi2)
            posperso.y=voi2;

                    SDL_BlitSurface(perso,NULL,ecran,&posperso);
              
            break;  


                 
           }

            }
      
        


    
SDL_Delay(10);
SDL_Flip(ecran);


}
SDL_Quit();
return 0;
         
clean(ecran,back,perso,voit,voit1,bus,tube,moto,bat,batj,batr,batt,bat1,source,destination);
}
